#include "symbol_table.h"
#include <cassert>
using namespace symbol_table;
using namespace std;

template <typename T>
SymbolTable<T>::SymbolTable() : scopes(vector<Scope>(1)) {}

template <typename T>
void SymbolTable<T>::EnterScope()
{
    scopes.push_back(Scope());
}

template <typename T>
void SymbolTable<T>::ExitScope()
{
    assert (scopes.size() > 1);
    scopes.pop_back();
}

template <typename T>
void SymbolTable<T>::Insert(string name, T info)
{
    Scope& scope = scopes.back();
    assert (!scope.count(name));
    scope[name] = info;
}

template <typename T>
T SymbolTable<T>::Lookup(string name)
{
    for (int i = scopes.size() - 1; i >= 0; --i)
    {
        Scope& scope = scopes[i];
        if (scope.count(name)) { return scope[name]; }
    }
    assert(false);
}

template <typename T>
void SymbolTable<T>::Clear()
{
    scopes = vector<Scope>(1);
}

template class SymbolTable<int>;